package com.seecen.HJM.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seecen.HJM.model.dto.BannerDto;
import com.seecen.HJM.model.entity.Banner;
import com.seecen.HJM.result.DataVo;
import com.seecen.HJM.result.PageResult;
import com.seecen.HJM.service.BannerService;
import com.seecen.HJM.service.MinioService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/banner")
@CacheConfig(cacheNames = "bannerCache")
//轮播图管理
public class BannerController {

/*
* 用到的类
* Vo:返回操作信息
* DTO:分页查询
* enum:轮播图状态
* */



    @Autowired
    private BannerService service;

    @Autowired
    private MinioService minioService;
    //分页查询
    @PostMapping("/getBanner")
    public DataVo<PageResult<Banner>> getBanner(@RequestBody BannerDto bannerDto){
        return service.getBanner(bannerDto);
    }

    //添加轮播图
    @PostMapping("/addBanner")
    public DataVo addBanner(@RequestParam("banner") MultipartFile banner){
        //获取url
        String bannerUrl = minioService.uploadFile(banner, "banners");
        return service.addBanner(bannerUrl);
    }

    //修改轮播图
    @PostMapping("/updateBanner/{id}")
        public DataVo updateBanner(@PathVariable("id") Long bannerId, @RequestParam("banner") MultipartFile banner){
        //获取url
        String bannerUrl = minioService.uploadFile(banner, "banners");
        return service.updateBanner(bannerId,bannerUrl);
    }

    //修改轮播图状态
    @PostMapping("/updateStatus/{id}")
    public DataVo updateStatus(@PathVariable("id") Long bannerId, @RequestParam("status") Integer bannerStatus){
        return service.updateStatus(bannerId,bannerStatus);
    }

    //删除轮播图
    @DeleteMapping("/deleteBanner/{id}")
    public DataVo deleteBanner(@PathVariable("id") Long bannerId){
        return service.deleteBanner(bannerId);
    }

    //批量删除
    @DeleteMapping("/deleteBannerList")
    public DataVo deleteBannerList(@RequestBody List<Long> bannerIds){
        return service.deleteBannerList(bannerIds);
    }


}
